package com.distribute.common;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

public class Constant {
	
	public static final long ADMIN_USER = 1L;
	
	public static final String COOKIE_NAME = "session_cookie";

	public static String SESSION_USER = "sessionUser";
	
	//点击登录按钮请求的url地址
	public static String DO_LOGIN_URL = "/login";
	
	public static String DO_LOGOUT_URL = "/logout";
	
	public static String LOGIN_PAGE_URL = "/login.html";
	
	public  static String FAVICONICO="/favicon.ico";
		
	public static String[]  DEFAULT_WHITE_URLS = {"/", DO_LOGIN_URL,  
			LOGIN_PAGE_URL, FAVICONICO, "/css/**", 
			"/js/**", "/fonts/**", "/layui/**", "/img/**",
			"/webjars/**", "/getCode", "/getCode2"};
	
	 /**
     * 客户端的重定向，将页面重定向为 path?ret=encodeURIComponent(window.location.href)
     */
    public static void clientRedirect(HttpServletResponse response, String path) throws IOException {
    	response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Type", "text/html;charset=utf-8");
        response.getWriter().println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
                + "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" + "<head>\n" + "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"/>\n"
                + "<title>跳转中...</title>\n" + "</head>\n" + "<body>\n" + "跳转中，请稍候...\n" + "<script type=\"text/javascript\">//<![CDATA[\n"
                + "window.location.href='" + path + "?ret='+encodeURIComponent(window.location.href);\n" + "//]]></script>\n" + "</body>\n" + "</html>\n");
    }

	public	static String checkJsonOrHtml(HttpServletRequest request) {
		String contentType = "text/html";
		String requestAccept = request.getHeader("accept");
        if (StringUtils.isNotEmpty(requestAccept)) {
            if (StringUtils.contains(requestAccept, "application/json") || StringUtils.contains(requestAccept, "text/javascript") || StringUtils
                    .contains(requestAccept, "text/json")) {
                contentType = "application/json";
            }
        }
		return contentType;
	}
	
}
